{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "线形平滑滤波器\n",
    "在模版尺寸加大的时候，噪声消除越好，但随之带来的是图像的模糊"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "vscode": {
     "languageId": "c"
    }
   },
   "outputs": [],
   "source": [
    "// 领域平均滤波法\n",
    "/********************************************************************\n",
    "* Function Name  : midvalue_filter_algorithm\n",
    "* Description    : Performs neighborhood averaging filter to smooth the image\n",
    "*                  by calculating the average grayscale value in a 3x3 template.\n",
    "* Input          : None\n",
    "* Output         : None\n",
    "* Return         : None\n",
    "********************************************************************/\n",
    "\n",
    "// Neighborhood Averaging Filter\n",
    "void midvalue_filter_algorithm(void) {\n",
    "    // Variables for iterating through the image pixels\n",
    "    int i = 0, j = 0;\n",
    "\n",
    "    // Loop through the image array starting from pixel (2, 0) to avoid boundary issues\n",
    "    for (i = 2; i < 136; i++) {\n",
    "        for (j = 0; j < 120; j++) {\n",
    "            // Load 3x3 neighborhood pixels into array `d`\n",
    "            d[0] = imagey[i][j];       // Center pixel\n",
    "            d[1] = imagey[i - 1][j - 1]; // Top-left\n",
    "            d[2] = imagey[i][j - 1];     // Left\n",
    "            d[3] = imagey[i + 1][j - 1]; // Bottom-left\n",
    "            d[4] = imagey[i - 1][j];     // Top\n",
    "            d[5] = imagey[i + 1][j];     // Bottom\n",
    "            d[6] = imagey[i - 1][j + 1]; // Top-right\n",
    "            d[7] = imagey[i][j + 1];     // Right\n",
    "            d[8] = imagey[i + 1][j + 1]; // Bottom-right\n",
    "\n",
    "            // Replace the center pixel with the average of the 3x3 neighborhood\n",
    "            imagey[i][j] = solve_average(d);\n",
    "        }\n",
    "    }\n",
    "\n",
    "    // Output the smoothed image\n",
    "    for (i = 0; i < 136; i++) {\n",
    "        for (j = 0; j < 120; j++) {\n",
    "            if (j < 119)\n",
    "                printf(\"%d,\", imagey[i][j]); // Print pixel value with a comma\n",
    "            else\n",
    "                printf(\"%d\", imagey[i][j]);  // Print last pixel in row without comma\n",
    "        }\n",
    "        printf(\";\"); // End of row\n",
    "    }\n",
    "    printf(\"\\r\\n\"); // Newline after image data\n",
    "}\n",
    "\n",
    "// 加权平均滤波法\n",
    "\n",
    "/********************************************************************************\n",
    "* Function Name  : midvalue_filter_algorithm\n",
    "* Description    : Performs weighted averaging filter to smooth the image while\n",
    "*                  emphasizing the center pixel more heavily.\n",
    "* Input          : None\n",
    "* Output         : None\n",
    "* Return         : None\n",
    "********************************************************************************/\n",
    "\n",
    "// Weighted Averaging Filter\n",
    "void midvalue_filter_algorithm(void) {\n",
    "    // Variables for iterating through the image pixels\n",
    "    int i = 0, j = 0;\n",
    "\n",
    "    // Loop through the image array starting from pixel (2, 0) to avoid boundary issues\n",
    "    for (i = 2; i < 136; i++) {\n",
    "        for (j = 0; j < 120; j++) {\n",
    "            // Load weighted 3x3 neighborhood pixels into array `d`\n",
    "            d[0] = 4 * imagey[i][j];       // Center pixel, weighted by 4\n",
    "            d[1] = imagey[i - 1][j - 1];   // Top-left, weight 1\n",
    "            d[2] = 2 * imagey[i][j - 1];   // Left, weight 2\n",
    "            d[3] = imagey[i + 1][j - 1];   // Bottom-left, weight 1\n",
    "            d[4] = 2 * imagey[i - 1][j];   // Top, weight 2\n",
    "            d[5] = 2 * imagey[i + 1][j];   // Bottom, weight 2\n",
    "            d[6] = imagey[i - 1][j + 1];   // Top-right, weight 1\n",
    "            d[7] = 2 * imagey[i][j + 1];   // Right, weight 2\n",
    "            d[8] = imagey[i + 1][j + 1];   // Bottom-right, weight 1\n",
    "\n",
    "            // Replace the center pixel with the weighted average of the 3x3 neighborhood\n",
    "            imagey[i][j] = solve_average(d);\n",
    "        }\n",
    "    }\n",
    "\n",
    "    // Output the smoothed image\n",
    "    for (i = 0; i < 136; i++) {\n",
    "        for (j = 0; j < 120; j++) {\n",
    "            if (j < 119)\n",
    "                printf(\"%d,\", imagey[i][j]); // Print pixel value with a comma\n",
    "            else\n",
    "                printf(\"%d\", imagey[i][j]);  // Print last pixel in row without comma\n",
    "        }\n",
    "        printf(\";\"); // End of row\n",
    "    }\n",
    "    printf(\"\\r\\n\"); // Newline after image data\n",
    "}\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
